package net.xdclass.controller;


import lombok.extern.slf4j.Slf4j;
import net.xdclass.controller.request.AccountLoginRequest;
import net.xdclass.controller.request.AccountRegisterRequest;
import net.xdclass.enums.BizCodeEnum;
import net.xdclass.service.AccountService;
import net.xdclass.service.FileService;
import net.xdclass.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

/**
 */
@RestController
@RequestMapping("/api/account/v1")
@Slf4j
public class AccountController {
    @Autowired
    private FileService fileService;
    @Autowired
    private AccountService accountService;

    /**
     * 文件上传超过1M会报错
     * @param file
     * @return
     */
    @PostMapping("upload")
    public JsonData uploadFile(@RequestPart("file") MultipartFile file){
        String result = fileService.uploadUserImage(file);
        return result !=null ? JsonData.buildSuccess(result) : JsonData.buildResult(BizCodeEnum.FILE_UPLOAD_USER_IMG_FAIL);

    }

    /**
     * 注册
     * @param request
     * @return
     */
    @PostMapping("register")
    public JsonData register(@RequestBody AccountRegisterRequest request){
        JsonData jsonData = accountService.register(request);
        return jsonData;
    }
    /**
     * 登录
     * @param request
     * @return
     */
    @PostMapping("login")
    public JsonData login(@RequestBody AccountLoginRequest request){
        JsonData jsonData = accountService.login(request);
        return jsonData;
    }

    /**
     * 查看个⼈信息
     * @return
     */
    @GetMapping("detail")
    public JsonData detail(){
        JsonData jsonData = accountService.detail();
        return jsonData;
    }

}

